#include "stm32f4xx.h"                  // Device header

#define MT6701Address						0x06<<1
uint8_t AngleDataAddress03[1]={0x03};
uint8_t AngleDataAddress04[1]={0x04};		

void MyI2C_Init()
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;


	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;           // 复用功能
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;      // 设置 GPIO 速度
	GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;         // 开漏输出
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;           // 上拉


	GPIO_Init(GPIOB, &GPIO_InitStruct);

	// 配置 GPIO 引脚为 I2C2 的复用功能
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); 
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); 
	
	
	I2C_InitTypeDef I2C_InitStruct;

	
	I2C_InitStruct.I2C_ClockSpeed = 100000;             // 时钟速度: 100kHz
	I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;             // I2C 模式
	I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;     // 占空比 2:1
	I2C_InitStruct.I2C_OwnAddress1 = 0x00;              // 主设备无需地址
	I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;            // 使能应答
	I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // 7位地址模式


	I2C_Init(I2C1, &I2C_InitStruct);


	I2C_Cmd(I2C1, ENABLE);
}

void MyI2C_SendData(I2C_TypeDef* I2Cx, uint8_t slaveAddr, uint8_t* data, uint16_t length)
{

    I2C_GenerateSTART(I2Cx, ENABLE);

    // 等待主模式选择
    while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));


    I2C_Send7bitAddress(I2Cx, slaveAddr, I2C_Direction_Transmitter);

    // 等待从机应答
    while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));


    for (uint16_t i = 0; i < length; i++)
    {
        I2C_SendData(I2Cx, data[i]);


        while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    }


    I2C_GenerateSTOP(I2Cx, ENABLE);
}

void MyI2C_ReceiveData(I2C_TypeDef* I2Cx, uint8_t slaveAddr, uint8_t* buffer, uint16_t length)
{

    I2C_GenerateSTART(I2Cx, ENABLE);


    while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));


    I2C_Send7bitAddress(I2Cx, slaveAddr, I2C_Direction_Receiver);


    while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));


    for (uint16_t i = 0; i < length; i++)
    {
        if (i == length - 1)
        {
            // 禁用应答
            I2C_AcknowledgeConfig(I2Cx, DISABLE);

            
            I2C_GenerateSTOP(I2Cx, ENABLE);
        }

        // 等待接收到数据
        while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));


        buffer[i] = I2C_ReceiveData(I2Cx);
    }

    // 重新使能应答（为下一次通信准备）
    I2C_AcknowledgeConfig(I2Cx, ENABLE);
}


uint8_t GetAngle(I2C_TypeDef* I2Cx)
{
	uint8_t AngleDataArr03[1]={0x00},AngleDataArr04[1]={0x00};
	uint8_t AngleData;
	MyI2C_SendData(I2Cx,MT6701Address,AngleDataAddress03,1);
	MyI2C_ReceiveData(I2Cx,MT6701Address,AngleDataArr03,1);
	MyI2C_SendData(I2Cx,MT6701Address,AngleDataAddress04,1);
	MyI2C_ReceiveData(I2Cx,MT6701Address,AngleDataArr04,1);
	
	AngleData=((AngleDataArr03[0]<<8)&AngleDataArr04[0])/360;
	return AngleData;
}
